package com.nakamachizu.controller.mixi;

import java.net.URLDecoder;
import java.util.logging.Logger;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.controller.validator.Validators;
import org.slim3.util.StringUtil;

import com.nakamachizu.util.ForwardUtil;

public class HelpController extends Controller {

    private static final Logger log = Logger.getLogger(HelpController.class.getName());
    
    private String ret;
    private String page;
    
    @Override
    public Navigation run() throws Exception {
        init();
        
        if(validate()){
            if(!StringUtil.isEmpty(ret)){
                ret = URLDecoder.decode(ret, "UTF-8");
                requestScope("ret", ret);
            }
            return forward(ForwardUtil.forwardJsp("mixi/help/" + page + ".jsp"));
        }else{
            return forward("/jsp/pc/mixi/error.jsp");
        }
        
        
    }
    
    private void init(){
        ret = requestScope("ret");
        page = requestScope("page");
    }
    
    protected boolean validate() {
        boolean result = false;
        Validators v = new Validators(request);
        v.add("page", v.required());
        v.add("page", v.regexp("^[a-z]+$"));
        if(!StringUtil.isEmpty(ret)){
            v.add("ret", v.regexp("^%2F[^/]+$"));
        }
        
        result = v.validate();
        
        return result;
    }
}
